package com.hongbao.game.service;

import com.hongbao.game.model.BatchPrize;
import com.hongbao.game.model.Prize;
import com.hongbao.game.model.PrizeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Service
public class PrizeService {
    @Autowired
    RedisTemplate redisTemplate;


    public List<BatchPrize> selectBatchPrizeList(List<Long> ids){
        //从数据库查询
        BatchPrize batchPrize1 = new BatchPrize("拼手气红包",
                10,
                new BigDecimal("100"),PrizeType.Random);

        BatchPrize batchPrize2 = new BatchPrize("固定红包",
                10,
                new BigDecimal("100"),PrizeType.Fix);

        BatchPrize batchPrize3 = new BatchPrize("冰箱",
                10,
                new BigDecimal("10"),PrizeType.Fix);
        List<BatchPrize> list = new ArrayList<>();
        list.add(batchPrize1);
        list.add(batchPrize2);
        list.add(batchPrize3);

        return list;

    }
    /**
     * @param batchPrize 一批奖品的信息
     *                   包括奖品名称，数量，总金额，类型（随机，固定）
     * @return 具体的奖品
     */
    public List<Prize> generatePrize(BatchPrize batchPrize){

        String name = batchPrize.getName();
        Integer count = batchPrize.getCount();
        BigDecimal totalAmount = batchPrize.getTotalAmount();
        PrizeType type = batchPrize.getType();

        List<Prize> prizeList = new ArrayList<>();

        if(type == PrizeType.Random){
            //1 随机生成count -1 个点，产生count个区间
            List<Double> randomList = new ArrayList<>();
            for (int i=0;i<count-1;i++){
                Random random = new Random();
                randomList.add(random.nextDouble());
            }
            //2排序
            Collections.sort(randomList);

            //3.分配具体的奖品
            BigDecimal alreayAmount = new BigDecimal("0");
            for(int i=0;i<randomList.size();i++){

                BigDecimal amount = totalAmount.multiply(
                        new BigDecimal(randomList.get(i)));
                Double doubleValue = amount.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();

                BigDecimal amountFixed2= new BigDecimal(doubleValue.toString());

                Prize prize = new Prize();
                prize.setName(name);
                prize.setAmount(amountFixed2.subtract(alreayAmount));
                alreayAmount = amountFixed2;

                prizeList.add(prize);
            }

            Prize lastPrize = new Prize();
            lastPrize.setName(name);
            lastPrize.setAmount(totalAmount.subtract(alreayAmount));
            prizeList.add(lastPrize);

        }else if(type == PrizeType.Fix){

            BigDecimal alreayAmount = new BigDecimal("0");
            for(int i=0;i<count-1;i++){
                Prize prize = new Prize();
                prize.setName(name);
                BigDecimal amount = totalAmount.divide(new BigDecimal(count));
                Double doubleValue = amount.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
                BigDecimal amountFixed2= new BigDecimal(doubleValue.toString());

                prize.setAmount(amountFixed2);

                alreayAmount = alreayAmount.add(amountFixed2);
                prizeList.add(prize);
            }
            Prize lastPrize = new Prize();
            lastPrize.setName(name);
            lastPrize.setAmount(totalAmount.subtract(alreayAmount));
            prizeList.add(lastPrize);
        }
        return prizeList;
    }


    public List<Prize> shufflePrize(List<BatchPrize> batchPrizeList){

        List<Prize> allPrize = new ArrayList<>();
        for(int i=0;i<batchPrizeList.size();i++){
            List<Prize> prizeList = generatePrize(batchPrizeList.get(i));

            /*prizeList.forEach((e)->{
                System.out.println(e);
            });*/

            BigDecimal sum = prizeList.stream()
                    .map((prize -> prize.getAmount()))
                    .reduce((a, b) -> a.add(b))
                    .get();

            System.out.println(sum);

            allPrize.addAll(prizeList);
        }

        Collections.shuffle(allPrize);

        return allPrize;
    }

}
